var api = require('../../utils/api.js');


Page({

    /**
     * 页面的初始数据
     */
    data: {
        descmsg: '',
        merch_name: '品牌',
        types_name: '类目',
        merch: [],
        merch_id: '',
        merch_ids: '',
        timea: '',
        timeb: '',
        endtime: '',
        strtime: '',
        lists: [],
        qdesc: 0,
        opid: '',
        mores: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var that = this;
        api.post({
            url: 'index/getallmerch',
            data: {},
            success: data => {
                console.log(data);
                that.setData({
                    merch: data.data.merch
                })
            }
        })
    },

    onShow() {
        this.getmyquoted();
    },

    getmyquoted() {

        var that = this;
        var strtime = this.data.strtime;
        var endtime = this.data.endtime;
        var merch_id = this.data.merch_ids;

        api.post({
            url: 'cars/getmyquoted',
            data: {
                strtime: strtime,
                endtime: endtime,
                merch_id: merch_id
            },
            success: data => {
                console.log(data);
                that.setData({
                    lists: data.data
                })

            }
        })


    },


    bindPickerChange(ret) {
        console.log(ret);
        var i = ret.detail.value;
        var merch = this.data.merch;

        console.log(merch[i].title)

        this.setData({
            merch_name: merch[i].title,
            merch_ids: merch[i].id,
            lists: []
        })

        this.getmyquoted();
    },

    addorder(ret) {
        console.log(ret);

        var id = ret.currentTarget.dataset.id;
        var title = ret.currentTarget.dataset.title;
        var merch_id = ret.currentTarget.dataset.merch_id;

        this.setData({
            mores: 1,
            merch_id: merch_id,
            merch_title: title,
            bj_id: id
        })

    },

    adddesc(ret) {
        console.log(ret);
        var id = ret.currentTarget.dataset.id;

        this.setData({
            opid: id,
            qdesc: 1
        })


    },
    closedesc() {
        this.setData({
            qdesc: 0
        })
    },

    getcardesc(ret) {
        this.setData({
            descmsg: ret.detail.value
        })
    },
    adddescb() {

        var str = this.data.descmsg;
        var id = this.data.opid;
        var that = this;

        if (!str) {
            wx.showToast({
                title: '请输入内容',
                icon: 'none'
            })

            return false;
        }

        api.post({
            url: 'cars/addcardesc',
            data: {
                desc: str,
                id: id
            },
            success: data => {
                console.log(data);
                that.setData({
                    qdesc: 0
                })

                that.getmyquoted();

            }
        })
    },
    closedescb() {
        this.setData({
            mores: 0
        })
    },

    //创建报价
    addcase() {

        this.setData({
            mores: 0
        })

        var id = this.data.bj_id;
        var merch_id = this.data.merch_id;
        var title = this.data.merch_title;
        wx.navigateTo({
            url: '/pages/project/index?id=' + id + '&title=' + title + '&merch_id=' + merch_id
        })
    },
    addquotaion() {
        wx.switchTab({
            url: '/pages/car/index',
        })
    },
    //删除
    deletebyid() {
        var id = this.data.bj_id;
        var that = this;
        that.setData({
            mores: 0
        })

        wx.showModal({
            title: '温馨提醒',
            content: '确认要删除此项？',
            complete: (res) => {
           
                if (res.confirm) {

                    api.post({
                        url: 'cars/delgoodforcarbyid',
                        data: {
                            id: id
                        },
                        success: data => {
                            console.log(data);

                            wx.showToast({
                              title: data.msg,
                              icon:'none'
                            })
                            that.getmyquoted();

                        }
                    })
                }
            }
        })


    },
    // 查看历史
    seelogs() {
        this.setData({
            mores: 0
        })

        var id = this.data.bj_id;

        wx.navigateTo({
            url: '/pages/quotaion/logs?id='+id
        })

    },
    //修改信息
    editquotaion(){
        this.setData({
            mores: 0
        })
        var id = this.data.bj_id;
        wx.navigateTo({
            url: '/pages/quotaion/edits?id='+id
        })

    } ,
    suoding(){
        var id = this.data.bj_id;
        api.post({
            url: 'cars/lockgoodforcarbyid',
            data: {
                id: id
            },
            success: data => {
                this.setData({
                    mores: 0
                })
            }
        })
    }


})